<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("modelConfigPath", "billing");
%>
<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style >
	#outStockInfoCopy{display:none}
	.thumbnail{margin: 0px}
	#logo{margin: 0px;padding: 0px;}
	#pic{padding: 0px;height:60px;width:60px;}
	#img{height:50px;width:50px;}
	td input{margin-top:-1px;margin-bottom:-1px;height:21px; width:98%;border:none;color:red}
	#area{
		height:420px;
		width:1097px;
		overflow-y:auto; 
		overflow-x:hidden; 
	}
</style>
	
<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">
	<div class="detailed-navbar-content panel-body">
		<input type="hidden" value='${stocksJson}' id="stocksJson" />
		<input type="hidden" value='${billingJson}' id="billingJson" />
		<t:form modelAttribute="billing" action="/stock/outStock/submitModifySave" method="post" cssClass="form-horizontal">
			<div class="hide">
				 <t:hidden path="billingId"/>
 				 <t:hidden path="deleteMark"/>	
 				 <t:hidden path="createUserName"/>
		 		 <t:hidden path="createUserId"/>
		 		 <t:hidden path="createDate"/>
		 		 <t:hidden path="shopId"/>
		 		 <t:hidden path="confirm"/>
		 		 <input type="hidden" value='${outStockId}' id="outStockId" name="outStockId"/>
			</div>
            <t:formgroup>
            	<t:label path="billingNo" cssClass="col-sm-1 col-xs-1" />
		        <t:input readonly="true" path="billingNo" colClass="col-sm-2 col-xs-2" />
                <t:label path="customerId" cssClass="col-sm-1 col-xs-1" />
                <t:select2 disabled="disabled" path="customerId" placeholder="请填写客户" allowClear="true" colClass="col-sm-2 col-xs-2"></t:select2>
                <t:label path="deliveryDate" cssClass="col-sm-1 col-xs-1" />
                <t:date disabled="disabled"  path="deliveryDate" colClass="col-sm-2 col-xs-2"></t:date>
                <t:label path="businessId" cssClass="col-sm-1 col-xs-1" />
                <t:select2 disabled="disabled" path="businessId" placeholder="业务员" allowClear="true" colClass="col-sm-2 col-xs-2"></t:select2>
            </t:formgroup>
            <div id="area">
            <table  class="cntable table table-bordered dataTable table-billing">
	            <thead>
	            	<th style="width:100px">序号</th>
	            	<th style="width:150px">商品编码</th>
	            	<th style="width:150px">商品名称</th>
	            	<th style="width:150px">规格</th>
	            	<th style="width:150px">单位</th>
	            	<th style="width:150px">售价</th>
	            	<th style="width:150px">欠货</th>
	            	<th>发货数</th>
	            	
	            </thead>
	            
	            <tbody id="outStockInfo">
	            	<c:set var="sequence"  value="0"/>
					<c:forEach var="outStockShow" items="${billing.outStock.outStockShows}">
						<c:forEach var="outStockCollocation" items="${outStockShow.outStockCollocations}" >
							<tr id="outStockCollocation_${sequence}">
								<td style="display:none"><input id="collocationId_${sequence}" name="collocationId" type="text" value="${outStockCollocation.collocationId}"/></td>
								<td style="display:none"><input id="cargoId_${sequence}" name="cargoId" type="text" value="${outStockShow.cargoId}"/></td>
								<td style="display:none"><input style="ime-mode:disabled" id="subtotal_${sequence}" name="subtotal" type="text" value="${outStockCollocation.realPrice*outStockCollocation.collocationNum}"/></td>
								<td>${sequence+1}</td>						
				            	<td><input style="ime-mode:disabled" disabled="disabled" id="cargoNo_${sequence}" name="cargoNo" type="text" value="${outStockShow.cargo.cargoNo}"></input></td>
				            	<td><input style="ime-mode:disabled" disabled="disabled" id="cargoName_${sequence}" name="cargoName" type="text" value="${outStockShow.cargo.cargoName}"/></td>
				            	<td><input style="ime-mode:disabled" disabled="disabled" id="propertyName_${sequence}" name="propertyName" type="text" value="${outStockCollocation.collocation.propertyNameOne}/${outStockCollocation.collocation.propertyNameTwo}"/></td>
				            	<td ><input style="ime-mode:disabled" disabled="disabled" id="unit_${sequence}" name="unit" type="text" value="${outStockCollocation.unit}"/></td>
								<td><input style="ime-mode:disabled" readonly="true" id="realPrice_${sequence}" name="realPrice" type="text" value="${outStockCollocation.realPrice}"/></td>
								<td><input style="ime-mode:disabled" readonly="true" id="collocationDebt_${sequence}"  name="collocationDebt" type="text" value="${outStockCollocation.collocationDebt}"/></td>
								<td><input style="ime-mode:disabled" onkeyup="return ValidateNumber(this,value)" id="collocationNum_${sequence}" name="collocationNum" type="text" value="${outStockCollocation.returnCargoNumCollocation}"/></td>
								
							</tr>
							<c:set var="sequence"  value="${sequence +1}"/>
						</c:forEach>
					</c:forEach>
				</tbody>
            </table>
			</div>
		</t:form>
	</div>
	<t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
		<t:button icon="icon-floppy-disk" onclick="saveData()" laddaType="progress" cssClass="btn-info" csize="sm">保存</t:button>
	</t:panelFoot>
</t:panel>

<script type="text/javascript">

$(function(){
	outStockInfo = $("#outStockInfoCopyBody").html();
	$('#pic').popover({
	     trigger : 'hover',//鼠标以上时触发弹出提示框
	     html:true,//开启html 为true的话，data-content里就能放html代码了
	     content:"<img src='<c:url value='/images/hu.jpg'/>' >"
	 });
	
	var productNo=$('#productNo').text();
	if(!$.isNull(productNo)){
	 showMark=true;
	 
	}
	
	$("[name='realPrice']").change(function(){
		/* alert($("[name='realPrice']").val()); */
	});
	$("#discount").change(function(){
		updateBillingMoney();
	});
	
	loadBilling();
})

function loadBilling(){
	num--;
	if(!$.isNull($("#billingJson").val())){
		var billing =JSON.parse($("#billingJson").val());

		billing.outStock.outStockShows.forEach(function(element,index){
			element.outStockCollocations.forEach(function(outStockCollocation,index){
				num++;
				$('#realPrice_'+num).val(outStockCollocation.realPrice);
				$('#collocationDebt_'+num).val(outStockCollocation.collocationDebt);
				updateChange(num);
				
			});
		});
	}
		
}

var outStockInfo;
var newOutStockInfo;
var num=0;
function addData(){
	num++;
	
	newOutStockInfo=outStockInfo.replace(/1/g,num+1);
	newOutStockInfo=newOutStockInfo.replace(/temp/g,num);

	$("#outStockInfo").append(newOutStockInfo);
	stocksShow(num);
}

function deleteData(deleteId){
	$("#"+deleteId).remove();
	var deleteNum = parseInt(deleteId.substring(20));
	updateBillingMoney();
}


function saveData(lBtn){
	var isSave=false;
	$("input[name='collocationId']").each(function(){
		if($(this).val()!=null && $(this).val() != ""){
			isSave=true;
		}
						
	});
	var returnCargoNum=false;
	$("input[name='collocationNum']").each(function(){
		if($(this).val()!= 0){
			returnCargoNum=true;
		}			
	});
	if(!isSave){
		$.showTips("请选择开单货品！",1000,"warning");
		return;
	}
	if(!returnCargoNum){
		$.showTips("请填写数量！",1000,"warning");
		return;
	}

	if (!$("form:first").valid()){
		return;
	}
	lBtn.start();
	$("form:first").submit();
}	

var showMark=true;

var stuffData=[];
function stocksShow(showNum){
	updateChange(showNum);
}

function updateChange(showNum){
	
	$('#collocationNum_'+showNum).bind('input propertychange', function() {
		var count = parseInt($(this).val());
		var collocationDebt=parseInt($('#collocationDebt_'+showNum).val());
		
		$(this).val(count);
		if(isNaN(count)){
			count = 0;
			$(this).val(0);
		}
		if(count>collocationDebt){
			$(this).val(collocationDebt);
		}
		var price = $('#realPrice_'+showNum).val();
		$('#subtotal_'+showNum).val(price*count);
		updateBillingMoney();
	});
}

function updateBillingMoney(){
	var billingMoney = 0;
	var discount = $("#discount").val();
	if(discount == "" || discount == null || discount == undefined || discount==0){
		$("input[name='subtotal']").each(function(){
			if($(this).val()!=null && $(this).val() != ""){
				billingMoney+= parseFloat($(this).val());
			}
							
		});

		$("#billingMoney").val(billingMoney);
    }else{
    	
    	$("input[name='subtotal']").each(function(){
    		if($(this).val()!=null && $(this).val() != ""){
				billingMoney+= parseFloat($(this).val());
			}			
		});
    	billingMoney=billingMoney*(discount/10);
		$("#billingMoney").val(billingMoney);
    }
}

function ValidateNumber(e, pnumber){
	if (!/^\d+[.]?\d*$/.test(pnumber))
	{
	e.value = /^\d+[.]?\d*/.exec(e.value);
	}else{
		
 	}
	return false;
}
</script>
<%@ include file="/WEB-INF/views/shared/base/scripts/jqueryui.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formtags.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formbuttons.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formpickers.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formselects.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formstyling.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>